namespace Mvc {
    using Castle.MicroKernel.Resolvers.SpecializedResolvers;
    using Castle.Windsor;
    using Castle.MicroKernel.Registration;

    using MvcTurbine.ComponentModel;
    using MvcTurbine.Windsor;
    using MvcTurbine.Web;
    using Services;

    public class DefaultMvcApplication : TurbineApplication {
        static DefaultMvcApplication() {
            // Get the default container to use with the application
            var defaultContainer = CreateWindsorContainer();

            // Pass in the initiated container to the service locator to use
            ServiceLocatorManager.SetLocatorProvider(() => new WindsorServiceLocator(defaultContainer));
        }

        /// <summary>
        /// Create the container and do custom initialization
        /// </summary>
        /// <returns></returns>
        static IWindsorContainer CreateWindsorContainer() {
            // Create the empty container to use
            WindsorContainer container = new WindsorContainer();

            // Add the array resolver to handle array types
            container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));

            // Register fluently
            // For more info, go to http://using.castleproject.org/display/IoC/Fluent+Registration+API
            container
                .Register(Component.For<IMessageService>()
                    .ImplementedBy<DefaultMessageService>()
                    .LifeStyle
                    .Singleton)

                .Register(AllTypes.Of<ISimpleMessageService>()
                    .FromAssembly(typeof(DefaultMvcApplication).Assembly)
                    .WithService
                    .FromInterface(typeof(ISimpleMessageService))
                    .Configure(c => c.LifeStyle.Transient));

            return container;
        }
    }
}
